Write a Program to Print Hello World

Posted in

Write a Program to Print Hello World
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    If you are learning any new programming language, the first program you probably write is to print Hello World or display any other message on the screen. It is a basic form of code that demonstrates how programming works and allows you to learn the syntax of a new programming language.

    In this article, we will write a program to print Hello World in different programming languages - C, C++, Python, and Java. However, you can display any other message on the screen, like Hello Programming, Welcome to the Tutorial, etc.

    Problem Statement

    The program to display a message on the screen is simple.

    We will print Hello World on the console window. All we need to do is use the console print statement in all four programming languages - C, C++, Python, and Java.

    1. C Hello World Program

    //Print Hello World 
    #include <stdio.h>
    
    int main()
    {	
    	printf("HELLO, WORLD!");
        return 0;
    }

    Output

    HELLO, WORLD!

    How Does Hello World Program in C Work?

    • The first line of the program, //Print Hello World , is the comment. In C, a single-line comment starts with '//'. A comment in programming aims to make the code easier to understand for humans. It provides information about the source code. However, the C compiler does not execute it.
    • Next, #include preprocessor directs the compiler to include the stdio.h file's content, which includes functions, macros, and variable types for input and output. The functions printf() displays the output on the screen and scanf() takes input from a user.
    • The program execution starts from the main( ) function.
    • The printf() function displays the formatted output on the screen. In the above program, it displays "HELLO, WORLD!".
    • Finally, return 0; indicates the end of the program.

    2. C++ Hello World Program

    //Print Hello World 
    #include<iostream>
    using namespace std;
    
    int main()
    {
    cout<<"HELLO, WORLD!";
    return 0;
    }

    Output

    HELLO, WORLD!

    How Does Hello World Program in C++ Work?

    • Like C, single-line comments in C++ start with '//'. We have started our program with a comment. However, a C++ compiler does not execute the command.
    • The #include preprocessor directive instructs a C++ compiler to include the content of the iostream file. This file provides input and output services for C++ programs. It provides cin to take user input and cout to display the output on the screen.
    • using namespace std; is a C++ directive that enables the use of all the names defined in 'std'.
    • The program starts executing from the main() function.
    • cout displays 'HELLO, WORLD!" on the screen.
    • return 0; indicates the end status of the program.

    3. Python Hello World Program

    #Print Hello World 
    print("HELLO, WORLD!")

    Output

    HELLO, WORLD!

    How Does Hello World Program in Python Work?

    Python has a simple and elegant syntax, which is like simple English. It does not require defining any preprocessor directive or file for input and output. It has a built-in function, print() , to display any message on the screen.

    • Single-line comments in Python start with '#".
    • The print() function displays 'HELLO, WORLD!' on the screen.

    4. Java Hello World Program

    //Print Hello World 
    class TechGeekBuzz {
        public static void main(String[] args) {
    
            System.out.println("HELLO, WORLD!"); 
        }
    }
    

    Output

    HELLO, WORLD!

    How Does Hello World Program in Java Work?

    • Like C and C++, single-line Java comments start with '//'. In our program, we have defined a single-line comment.
    • every Java program or application begins with a class, as it is a class-based programming language. The name of the class we used in the above program is 'TechGeekBuzz'. Make sure to save your source code with the same name as that of the class.
    • public static void main(String[] args) is the main function in Java, and the program starts executing from here. Every Java program should contain the main method inside the class definition.
    • System.out.println("Hello, World!"); prints "HELLO, WORLD!" on the screen.

    Conclusion

    Writing a program to print Hello World in any programming language is the most basic. Every beginner writes and executes this program to learn the syntax of the language.

    If you are also a beginner in programming, you can take the help of the above programs and write your own program to display any message on the screen. Also, we have explained how each program works to help you understand what each line in the code is intended for.

    People are also reading:

    Leave a Comment on this Post

    0 Comments